package com.aigc.controller;

import com.aigc.config.KnowledgeBase;
import com.aigc.service.ChatHistoryService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author lhf
 */
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    private ChatClient chatClient;
    @Autowired
    private ChatHistoryService chatHistoryService;
    @Autowired
    private KnowledgeBase knowledgeBase;
    @PostMapping("/chat")
    public Flux<String>  chat(@RequestParam("prompt") String prompt,@RequestParam("chatId") String chatId){
        knowledgeBase.getKnowledgeBase();
        chatHistoryService.addChatHistory("chat",chatId);
        return chatClient.prompt(prompt)
                .advisors(a -> {
                    a.param(ChatMemory.CONVERSATION_ID, chatId);
                })
                .stream()
                .content();
    }
}
